<h1>
	<? if(isset($product['Product']['id'])): ?>
		Izmena proizvoda #<?= $product['Product']['id'] ?>
	<? else: ?>
		Novi proizvod
	<? endif; ?>
</h1>

<?= $this->Form->create('Product', array('url'=>'/admin_products/save', 'enctype'=>'multipart/form-data')) ?>
	<?= $this->Form->hidden('id', array('value'=>@$product['Product']['id'])) ?>
	
	<?= $this->Form->label('category_id', 'Kategorija:') ?>
	<?= $this->Form->select('category_id', $allcategories, array('value'=>@$product['Product']['category_id'], 'empty'=>false)) ?>
	
	<?= $this->Form->label('name', 'Naziv:') ?>
	<?= $this->Form->text('name', array('value'=>@$product['Product']['name'], 'class'=>'large')) ?>
	
	<?= $this->Form->textarea('description', array('value'=>@$product['Product']['description'], 'class'=>'editable')) ?>
	
	<fieldset>
		<legend>Slike</legend>
		
		<ul class="file-list">
			<li class="control">
				<?= $this->Form->file('ProductImage.-1.file') ?>
				<?= $this->Form->hidden('ProductImage.-1.position') ?>
				<span>+Dodaj</span>
			</li>
			<? if (! empty($product['ProductImage'])): ?>
    			<? foreach($product['ProductImage'] as $k=>$image): ?>
    			<li class="item">
    				<span class="handle"></span>
    				<a class="delete" href="javascript:void(0)" onclick="$(this).parent().remove()">x</a>
    				<?= $this->Form->hidden("ProductImage.{$k}.position", array('value'=>$image['position'])) ?>
    				<?= $this->Form->hidden("ProductImage.{$k}.src", array('value'=>$image['src'])) ?>
    				<?= $this->Html->image("products/".$product["Product"]["id"]."/".$image["src"], array('height'=>70)) ?>
    			</li>
    			<? endforeach; ?>
    		<? endif ?>
		</ul>
	</fieldset>
	
	<br clear="all">
	<?= $this->Form->submit('Sačuvaj', array('div'=>false)) ?>
	<?= $this->Form->submit('Poništi', array('onclick'=>"location.href='/admin_products'", 'type'=>'button', 'div'=>false)) ?>
<?= $this->Form->end() ?>

<script type="text/javascript">
function AddReceipe() {
	var d = $('#receipes-dialog');
	if(d.length){
		d.dialog('open');
	}else{
		d = $('<form action="javascript:void(0)" id="receipes-dialog">');
		d.submit(function(){
			var val = $('#receipes-kword').val().replace(/^[\s\r\t\n]+|[\s\r\n\t]+$/g, "");
			if(val.length<3){
				return alert("Morate uneti minimum 3 karaktera.")
			}
	
			var ul_destination = $('#product-receipes');
			var receipe_ids = []
			
			ul_destination.find("input[type=hidden]").each(function(){
				receipe_ids.push(this.value)
			});
			
			var ul = $('#receipes-search-list');
			$.ajax({
				url: '/admin_receipes/search/'+val,
				data: {receipe_ids: receipe_ids},
				type: 'post',
				beforeSend: function(){
					ul.empty().addClass('loading');
				},
				complete: function(req){
					ul.removeClass('loading');
	
					if(req.status == 200) {
						var data = eval('('+req.responseText+')');
						if(!data){
							return
						}
	
						if(data.length>0) {
							var li;
							for(var i=0; i<data.length; i++){
								receipe = data[i]
								li = $('<li>').appendTo(ul);
								li.append('<span class="title">'+receipe.title+'</span>');
								li.append('<img src="/img/recepti/'+receipe.image+'">');
								li.data("receipe", receipe);
								li.click(function(){
									var receipe = $(this).data("receipe");
									if(!ul_destination.find("input[value="+receipe.id+"]").length) {
										var nli = $('<li>');
										var index = -1 * ul_destination.find("li").length;
										nli.append('<a class="delete" onclick="$(this).parent().remove()" href="javascript:void(0)">x</a>');
										nli.append('<input type="hidden" id="Receipe'+index+'ReceipeId" value="'+receipe.id+'" name="data[Receipe]['+index+'][receipe_id]">');
										nli.append('<span class="title">'+receipe.title+'</span>');
										nli.append('<img height="70" alt="" src="/img/recepti/'+receipe.image+'">');
										ul_destination.append(nli);
										li.remove();
									}
								});
							}
						}else{
							ul.append('<li class="no-results">Nije pronađen nijedan recept.</li>');
						}
					}else{
						alert(req.status + "." + req.statusText);
					}
				}
			});
		});
		d.append('<input type="text" id="receipes-kword" style="width:80%" placeholder="Traži...">');
		d.append('<input type="submit" value="Traži" style="width:17%;margin-left:1%">');
		d.append('<ul id="receipes-search-list" class="clean">');
	
		d.dialog({
			width:600,
			height:400,
			title:'Pretraga recepata'
		});
	}
}
</script>